//
//  GuideViewController.m
//  e-nurse
//
//  Created by  on 12/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GuideViewController.h"
#import "AppDelegate.h"
#import "ImageGuideViewController.h"
#import "LastGuideViewController.h"

@interface GuideViewController (){
@private
    BOOL _isScrollViewScolling;
}

@property (strong, nonatomic) NSMutableArray *guides;
@property (strong, nonatomic) UIImage *normalPageControlDot;
@property (strong, nonatomic) UIImage *highlightPageControlDot;

- (void)updateDotColorOfPageControl;

@end

@implementation GuideViewController
@synthesize pageControl = _pageControl;
@synthesize scrollView = _scrollView;
@synthesize appDelegate = _appDelegate;
@synthesize guides = _guides;
@synthesize normalPageControlDot = _normalPageControlDot;
@synthesize highlightPageControlDot = _highlightPageControlDot;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    self.appDelegate = nil;
    self.guides = nil;
    self.normalPageControlDot = nil;
    self.highlightPageControlDot = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.normalPageControlDot = [UIImage imageNamed:@"daohangpoint.png"];
    self.highlightPageControlDot = [UIImage imageNamed:@"daohangpoint2.png"];
    
    self.guides = [[[NSMutableArray alloc] init] autorelease];
    
    for(int i = 1; i < 4; ++i)
    {
        ImageGuideViewController *imageGuide = [[ImageGuideViewController alloc] initWithNibName:@"ImageGuideViewController" bundle:nil];
        imageGuide.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%i.png", i]];
        [self.guides addObject:imageGuide];
        [imageGuide release];
    }
    
    LastGuideViewController *lastGuide = [[LastGuideViewController alloc] initWithNibName:@"LastGuideViewController" bundle:nil];
    [lastGuide setGuideViewController:self];
    [self.guides addObject:lastGuide];
    [lastGuide release];
    
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [self.guides count], self.scrollView.frame.size.height);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.scrollsToTop = NO;
    self.scrollView.delegate = self;

    [self.pageControl setNumberOfPages:[self.guides count]];
    [self loadScrollViewWithPage:(self.pageControl.currentPage - 1)];
    [self loadScrollViewWithPage:(self.pageControl.currentPage)];
    [self loadScrollViewWithPage:(self.pageControl.currentPage + 1)];
    [self.pageControl setCurrentPage:0];
    [self updateDotColorOfPageControl];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    self.pageControl = nil;
    self.scrollView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)updateDotColorOfPageControl
{
    NSArray *subViews = self.pageControl.subviews;
    
    for (int i = 0; i < [subViews count]; ++i) 
    {
        UIImageView *dot = [subViews objectAtIndex:i];
        dot.image = (self.pageControl.currentPage == i ? self.highlightPageControlDot : self.normalPageControlDot);
    }
}

- (void)loadScrollViewWithPage:(int)page 
{
    if (page < 0 || page >= self.pageControl.numberOfPages) return;
	
    // replace the placeholder if necessary
    
    UIViewController *controller = [self.guides objectAtIndex:page];
	
    if (controller.view.superview == nil) 
    {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [self.scrollView addSubview:controller.view];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{

    if (_isScrollViewScolling) 
    {
        return;
    }
    
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
	
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
    
    [self updateDotColorOfPageControl];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    _isScrollViewScolling = NO;
}

- (void)scrollToPage:(int)page
{
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
    
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:YES];
    
    _isScrollViewScolling = true;   
}

- (void)startApp
{
    [self.appDelegate startApp];
}

@end
